include( "shared.lua" )

function ENT:Initialize()
	self.Width, self.Height = 10, 10
	
	self.Options = {}
	self:AddOption( "New Game", function() end )
	self:AddOption( "Load Game", function() end )
	self:AddOption( "Save Game", function() end )
	self:AddOption( "Exit", function() end )
end
function ENT:Draw()
	local here = self:GetPos() + Vector( 0, 0, 50 )
	local ang = ( EyePos() - here ):Angle()
		ang.p = 90
	ang:RotateAroundAxis( ang:Up(), 90 )
	
	cam.Start3D2D( here, ang, 0.5 )
		draw.RoundedBox(
			4,
			self.Width * -0.5, self.Height * -0.5,
			self.Width, self.Height,
			Color( 0, 0, 0, 200 )
		)
		self:DrawMenu( self.Width * -0.5, self.Height * -0.5, self.Width, self.Height )
	cam.End3D2D()
end

function ENT:CursorPos()
	local here = self:GetPos() + Vector( 0, 0, 50 )
	local eyepos = EyePos()
	local eyenorm = EyeAngles():Forward()
	local ang = ( EyePos() - here ):Angle()
		ang.p, ang.r = 0, 0
	local cos = eyenorm:Dot( ang:Forward() )	
	
	if ( cos == 0 ) then return 0 end // The ray will never intersect.
	local deltaD = here:Length() - eyepos:Length()
	
	local point = eyepos + ( eyenorm * ( ( deltaD / cos ) * -1 ) )
	self.Entity:SetAngles( ang )
	local localPos = self.Entity:WorldToLocal( point - Vector( 0, 0, 50 ) ) * 2

	return localPos.y, localPos.z * -1
end
function ENT:AddOption( name, func )
	self.Options[ #self.Options + 1 ] = { name, func }
	
	surface.SetFont( "Default" )
	local w = surface.GetTextSize( name ) + 10
	
	self.Width = math.max( self.Width, w )
	self.Height = self.Height + 15
end
function ENT:DrawMenu( x, y, w, h )
	local mX, mY = self:CursorPos()
	
	local xON = mX > x && mX < x + w
	
	y = y + 5
	for _, line in pairs( self.Options ) do
		local col = Color( 255, 255, 255, 200 )
		if ( xON && mY > y && mY < y + 15 ) then col = Color( 255, 255, 0, 200 ) end
		draw.SimpleText(
			line[ 1 ],
			"Default",
			x + ( w / 2 ), y,
			col,
			TEXT_ALIGN_CENTER
		)
		y = y + 15
	end
end
